package com.example.shop_car_2208.register


import android.content.Intent
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.R
import com.example.shop_car_2208.WebViewActivity2
import com.example.shop_car_2208.databinding.ActivityRegisterBinding
import com.example.shop_car_2208.login.LofinUserActivity
import com.example.shop_car_2208.mvvm.BaseActivity
import com.example.shop_car_2208.mvvm.BaseViewModel
import com.example.shop_car_2208.mvvmdemo.LoginActivity
@Route(path = "/app/register")
class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        viewModel.registerSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code == 200){
                val intent = Intent(this,LofinUserActivity::class.java)
                intent.putExtra("username",binding.et1.text.toString())
                intent.putExtra("password",binding.et2.text.toString())
                setResult(102,intent)
                finish()
            }
        }
    }

    override fun initView() {
        binding.btn.isEnabled = false
        binding.tt.setOnCheckedChangeListener { buttonView, isChecked ->
             binding.btn.isEnabled = binding.tt.isChecked

        }
       binding.btn.setOnClickListener {
           viewModel.register(binding.et1.text.toString(),
           binding.et2.text.toString(),
           binding.et3.text.toString())
       }
        val spann = SpannableStringBuilder(binding.tt.text.toString())
        spann.setSpan(ForegroundColorSpan(Color.YELLOW),7,13,SpannableStringBuilder.SPAN_EXCLUSIVE_INCLUSIVE)
        spann.setSpan(object : ClickableSpan(){
            override fun onClick(widget: View) {
               // ToastUtils.showLong("点击了复选框")
                val intent = Intent(this@RegisterActivity,WebViewActivity2::class.java)
                intent.putExtra("url","https://m.qfang.com/shenzhen/user/privacy-protocol.html")
                startActivity(intent)
            }

        },14,20,SpannableStringBuilder.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tt.movementMethod = LinkMovementMethod.getInstance()
        binding.tt.setText(spann)
    }

    override fun getViewModel(): Class<RegisterViewModel> {
        return RegisterViewModel::class.java
    }

    override fun getLayout(): Int {
       return R.layout.activity_register
    }

}